package concordia.comp6231.failuredetection;

import java.util.ArrayList;
import java.util.List;

import concordia.comp6231.common.ApplicationConfig;
import concordia.comp6231.rfmulticast.ListernerHandler;
import concordia.comp6231.rfmulticast.Message;
import concordia.comp6231.rfmulticast.RFIFOMulticast;
import concordia.comp6231.rfmulticast.ResultHandler;
import static concordia.comp6231.failuredetection.FailureDetector.*;

public class FailureDetectorListener implements ListernerHandler {
	
	public FailureDetectorListener(){
	}

	@Override
	public Message handleResult(Message msg) {
		RFIFOMulticast rfm = null;
		String msgType = msg.get(MSG_TYPE);
		if (MSG_HEART_BEAT.equals(msgType)) {
			System.out.println("INFO: Heartbeat request received.");
			List<String> tmp = new ArrayList<String>();
			tmp.add(msg.get(RFIFOMulticast.RFM_MSG_SENDER));
			rfm = new RFIFOMulticast(tmp);
			Message msgAlive = new Message();
			msgAlive.put(MSG_HEART_BEAT_RESPONSE, MSG_ALIVE);
			msgAlive.put(MSG_TYPE, MSG_HEART_BEAT_RESPONSE);
			return msgAlive;
		} 
		else if (MSG_SUSPECT.equals(msgType)) {
			// 2
			System.out.println("INFO: Suspect request received to suspect a member");
			List<String> tmp = new ArrayList<String>();
			tmp.add(msg.get(MSG_SUSPECTED_MEMBER));
			rfm = new RFIFOMulticast(tmp);
			Message msgSuspect = new Message();
			msgSuspect.put(MSG_TYPE, MSG_SUSPECT_CONFIRM);//= Are you dead?
			msgSuspect.put(MSG_ORIGINAL_SENDER,
					msg.get(RFIFOMulticast.RFM_MSG_SENDER));
			System.out.println("INFO: Request will be sent to suspected member");
			Message resultMsg = rfm.synchronosSend(
					ApplicationConfig.FAILUREDETECTION_SERVER_PORT_SEND, msgSuspect);
			Message myResultMsg = new Message();
			System.out.println("INFO: Replay was received from the side of suspected member, let us see what it is");
			if (MSG_SUSPECT_CONFIRM_RESPONSE
					.equals(resultMsg.get(msgType))) {
				myResultMsg.put(MSG_TYPE, MSG_SUSPECT_REJECT);
				String sender = resultMsg.get(RFIFOMulticast.RFM_MSG_SENDER);
				String originalSender = resultMsg.get(MSG_ORIGINAL_SENDER);
				System.out.println("INFO: Request from " + originalSender+ "to suspect "+ sender + " and he is still alive.");
				return myResultMsg;
			} 
			else if (resultMsg.get(RFIFOMulticast.RFM_MSG_DEAD_HOST)!= null && !(resultMsg.get(RFIFOMulticast.RFM_MSG_DEAD_HOST)).isEmpty() ) {
				System.out.println("No response from suspected member");
				myResultMsg.put(MSG_TYPE, MSG_SUSPECT_ACK);
				System.out.println("INFO: Request from ? to suspect "+ resultMsg.get(RFIFOMulticast.RFM_MSG_DEAD_HOST) + " and he is dead.");
				return myResultMsg;
			}
		} 
		else if (msg.get(MSG_TYPE).equals(MSG_SUSPECT_CONFIRM)) {
			//String sender = msg.get(msg.get(RFIFOMulticast.RFM_MSG_SENDER));
			String originalSender = msg.get(msg.get(MSG_ORIGINAL_SENDER));
			Message response = new Message();
			response.put(MSG_TYPE, MSG_SUSPECT_CONFIRM_RESPONSE);
			response.put(MSG_ORIGINAL_SENDER, originalSender);
			return response;
		}
		return null;
	}

}
